package code.main.controller.table
{
	import code.app.AppCtrlList;
	import code.app.AppDmList;
	import code.app.AppView;
	import code.main.controller.event.EventManager;
	import code.main.controller.event.EventName;
	import code.main.controller.event.MyEvent;
	import code.main.modle.config.ConfigData;
	import code.main.modle.dnAlg.DnAlg;
	import code.main.modle.lang.Language;
	import code.main.modle.table.CardVo;
	import code.main.modle.table.Player;
	import code.main.modle.table.TableData;
	import code.module.chat.TableChat;
	import code.module.motion.MotionCtrl;
	import code.module.sound.SoundManager;
	import code.net.CommandTyte;
	import code.net.SocketManager;
	
	import com.yy.mvc.ctrl.Controller;
	import com.yy.net.event.EventCenter;
	import com.yy.net.event.SocketEvent;
	import com.yy.net.socketclass.MySocket;
	
	import flash.events.Event;
	
	/**
	 *桌子管理器 
	 * @author YY
	 * 
	 */
	public class TableCtrl extends Controller
	{
		private var _sm:SocketManager;
		private var _seatCtrl:SeatCtrl;
		private var _dealCardCtrl:DealCardCtrl;//发牌动画控制器
		private var _tableChat:TableChat;//聊天控制器
		
		private var _player:Player;//玩家信息
		private var _tableData:TableData;//桌子信息
		private var _grabBankLeaveTime:int;//抢庄剩余时间
		private var _betLeaveTime:int;//下注 剩余时间
		private var _selectCardLeaveTime:int;//选牌剩余时间
		public function TableCtrl()
		{
			init();
		}
		private function init():void {
			_player = AppDmList.getInstance().player;
			_tableData = AppDmList.getInstance().tableData;
			//初始化控制器
			_seatCtrl = new SeatCtrl(AppView.getInstance().tableLayer, seatCtrlCb);
			_dealCardCtrl = new DealCardCtrl(AppView.getInstance().tableLayer, dealCardCB);
			_tableChat = new TableChat(AppView.getInstance().tableLayer, chatCB);
			//注册命令
			_sm = SocketManager.getInstance();
			_sm.register(CommandTyte.RECEIVE_ERROR_CODE, recErrorCode);
			_sm.register(CommandTyte.RECEIVE_PLAYER_IN_ROOM, recPlayerInRoom);
			_sm.register(CommandTyte.RECEIVE_TABLE_INFO, recTableInfo);
			_sm.register(CommandTyte.RECEIVE_PLAYER_SEAT, recPlayerSeat);
			_sm.register(CommandTyte.RECEIVE_CAN_GRAB_BANKER, recCanGrabBanker);
			_sm.register(CommandTyte.RECEIVE_PALYER_GRAB_BANKER, recPlayerGrabBanker);
			_sm.register(CommandTyte.RECEIVE_BANKER, recBanker);
			_sm.register(CommandTyte.RECEIVE_CAN_BET, recCanBet);
			_sm.register(CommandTyte.RECEIVE_PLAYER_BET, recPlayerBet);
			_sm.register(CommandTyte.RECEIVE_GET_CARD, recGetCard);
			_sm.register(CommandTyte.RECEIVE_CAN_SELECT_CARD, recCanSelectCard);
			_sm.register(CommandTyte.RECEIVE_PLAYER_COW_NUMB, recPlayerCowNumb);
			_sm.register(CommandTyte.RECEIVE_GAME_RESULT, recGameResult);
			_sm.register(CommandTyte.RECEIVE_RECONNECT_INFO, recReconnectInfo);
			_sm.register(CommandTyte.RECEIVE_PLAY_DEAL_CARD, recPlayDealCard);
			_sm.register(CommandTyte.RECEIVE_PLAY_UP, recPlayUp);
			_sm.register(CommandTyte.RECEIVE_PLAY_QUIT, recPlayQuit);
			_sm.register(CommandTyte.RECEIVE_PLAY_CHAT, recPlayChat);
			_sm.register(CommandTyte.RECEIVE_FACE_EMOTION, recFaceEmotion);
			addSocketEvent();
		}
		
		/**
		 *座位控制器回调 
		 * @param type
		 * @param data
		 * 
		 */
		private function seatCtrlCb(type:int, data:Object=null):void {
			switch(type) {
				//坐下
				case SeatCtrl.OPTION_TYPE_CLICK_SEAT_BTN:
					if(!_player.isSeat) {
						_sm.seat(data.money, data.id);
					}else {
						_seatCtrl.playerSeatEffect(_player.seatId);
					}
					break;
				//抢庄
				case SeatCtrl.OPTION_TYPE_CLICK_GRAB_BANKER:
					if(_tableData.currentGameState == TableData.GAME_STATE_GRAB_BANK) {
						_sm.grabBank(_player.seatId, true);
					}
					break;
				//放弃抢庄
				case SeatCtrl.OPTION_TYPE_CLICK_GIVE_UP_BANK:
					if(_tableData.currentGameState == TableData.GAME_STATE_GRAB_BANK) {
						_sm.grabBank(_player.seatId, false);
					}
					break;
				//下注
				case SeatCtrl.OPTION_TYPE_CLICK_BET:
					if(_tableData.currentGameState == TableData.GAME_STATE_BET) {
						_sm.bet(int(_player.gameId), _player.seatId, data.value);
					}
					break;
				//点击牌
				case SeatCtrl.OPTION_TYPE_CLICK_CARD:
					clickCardFunc();
					break;
				//点击亮牌
				case SeatCtrl.OPTION_TYPE_CLICK_SHOW_CARD:
					if(_tableData.currentGameState == TableData.GAME_STATE_SHOW_CARD) {
						clickShowCard();
					}
					break;
				//结算完毕
				case SeatCtrl.OPTION_TYPE_SETTLEMENT_COMPLETE:
					if(_tableData.currentGameState == TableData.GAME_STATE_SETTLEMENT) {
						_sm.settlementOk();
					}
					break;
				case SeatCtrl.OPTION_TYPE_CLICK_UP_SEAT://站起
					clickUpSeatFunc();
					break;
				case SeatCtrl.OPTION_TYPE_CLICK_GO_HALL://大厅
					clickGoHallFunc();
					break;
			}
		}
		
		/**
		 *聊天回调 
		 */
		public function chatCB(type:int, data:Object):void {
			switch(type) {
				case TableChat.SEND_CHAT_MSG:
					_sm.sendChat(data.msg);
					break;
			}
		}
		
		/**
		 *点击站起 
		 */
		private function clickUpSeatFunc():void {
			_sm.upSeat();
		}
		/**
		 *点击返回大厅 
		 */
		private function clickGoHallFunc():void {
			_sm.goHall();
		}
		
		/**
		 *点击亮牌 
		 */
		private function clickShowCard():void {
			trace("点击亮牌");
			_sm.showCard(int(_player.gameId), _player.seatId, _player.getSelectCards(), _player.getCowNumb());
		}
		
		/**
		 *点击选牌 
		 */
		private function clickCardFunc():void {
			var cowValue:int = DnAlg.getInstance().getCowValue(_player.cards);
			_seatCtrl.showCowUi(true, _player.seatId, cowValue);
			_seatCtrl.showCards(_player.cards, _player.seatId);
			if(_player.getSelectCardNumb() == 3 || _player.getSelectCardNumb() == 2) {
				_seatCtrl.showShowCardBtn(true, true);
			}else {
				_seatCtrl.showShowCardBtn(true, false);
			}
			
		}
		////////////////////////////////////////收到服务器命令////////////////////////////////////////////////////////		
		
		private function recFaceEmotion(socket:MySocket):void {
			trace("========>收到用户表情");
			socket.readbegin();
			var uid:int = socket.readInt();
			var motionId:int = socket.readByte();
			trace("用户id：" + uid);
			trace("表情id：" + motionId);
			var me:MyEvent = new MyEvent(EventName.MOTION_RECEIVE_MOTION);
			me.objData = {uid:uid, motionId:motionId};
			EventManager.getInstance().dispatcher(me);
			trace("<===========表情接收完毕");
		}
		
		/**
		 *收到用户站起 
		 * @param socket
		 */
		private function recPlayUp(socket:MySocket):void {
			trace("==========>收到用户站起:");
			socket.readbegin();
			var seatId:int = socket.readByte();
			var p:Player = _tableData.getMatchPlayerBySeatId(seatId);
			if(p) {
				//设置数据
				p.upSeat();
				_tableData.removeAMatchPlayer(int(p.gameId));
				_tableData.addAWatchPlayer(p);
				//刷新ui
				_seatCtrl.hideASeat(seatId);
				//
				_seatCtrl.hideClock();
				_seatCtrl.hicePublicClock();
				_seatCtrl.showGrabBankerUi(false);
			}
			trace("<===========接收完毕");
		}
		
		/**
		 *收到用户退出房间 
		 * @param socket
		 */
		private function recPlayQuit(socket:MySocket):void {
			trace("==========>收到用户离开房间");
			socket.readbegin();
			var gameId:int = socket.readInt();
			var isMatch:Boolean = false;
			var p:Player = _tableData.getWatchPlayerById(gameId);
			if(!p) {
				p = _tableData.getMatchPlyerById(gameId);
				isMatch = true;
			}
			if(p) {
				//设置数据
				if(isMatch) {
					_tableData.removeAMatchPlayer(gameId);
					_seatCtrl.hideASeat(p.seatId);
				}else {
					_tableData.removeAWatchPlayer(gameId);
				}
				//刷新界面
				if(gameId == int(_player.gameId)) {
					//返回大厅
					goHall();
				}
			}
			trace("<===========接收完毕");
		}
		
		/**
		 *收到聊天 
		 * @param socket
		 */
		private function recPlayChat(socket:MySocket):void {
			trace("======> 收到聊天消息");
			socket.readbegin();
			var n:String = socket.readString();
			var msg:String = socket.readString();
			_tableChat.showMsg(n, msg);
			trace("<======接收完毕");
		}
		/**
		 *收到错误码 
		 * @param socket
		 */
		private function recErrorCode(socket:MySocket):void {
			socket.readbegin();
			var code:int = socket.readByte();
			trace("===>错误代码" + code);
			switch(code) {
				case 0:
					break;
				case 1:
					break;
				case 2:
					break;
				case 3:
					break;
				case 4:
					break;
				case 5:
					break;
				case 6:
					break;
				case 7:
					break;
				case 8:
					break;
				case 9:
					break;
				case 10:
					break;
			}
		}
		/**
		 *收到用户进入房间 
		 * @param socket
		 */
		private function recPlayerInRoom(socket:MySocket):void{
			trace("====>收到用户进入房间 ");
			socket.readbegin();
			var id:String = String(socket.readInt());
			var player:Player;
			if(id == _player.gameId) {
				player = _player;
			}else {
				player = new Player();
			}
			player.gameId = id;
			player.level = socket.readByte();
			player.titleId = socket.readByte();
			player.seatId = socket.readByte();
			player.gender = socket.readByte();
			player.vipLevel = socket.readByte();
			player.exp = socket.readInt();
			player.giftId = socket.readInt();
			player.tableMoney = socket.readDouble();
			player.userName = socket.readString();
			player.pic = socket.readString();
			
			_tableData.addAWatchPlayer(player);
		}
		/**
		 *收到房间列表
		 * @param socket
		 */
		private function recTableInfo(socket:MySocket):void{
			trace("====>收到房间列表");
			socket.readbegin();
			var state:int = socket.readByte();
			_tableData.currentGameState = state;
			var len:int = socket.readByte();
			var id:String;
			var i:int;
			var j:int;
			var player:Player;
			for(i=0; i<len; i++) {
				id = String(socket.readInt());
				if(id == _player.gameId) {
					player = _player;
				}else {
					player = new Player();
				}
				player.gameId = id;
				player.isGaming = socket.readByte() == 1;
				player.isBanker = socket.readByte() == 1;
				player.level = socket.readByte();
				player.titleId = socket.readByte();
				player.seatId = socket.readByte();
				player.gender = socket.readByte();
				player.vipLevel = socket.readByte();
				player.exp = socket.readInt();
				player.giftId = socket.readInt();
				player.tableMoney = socket.readDouble();
				player.gameMoney = socket.readDouble();
				player.userName = socket.readString();
				player.pic = socket.readString();
				if(player.seatId == 10) {
					_tableData.addAWatchPlayer(player);
				}else {
					_tableData.addAMatchPlayer(player);
				}
				trace("gameId:"+player.gameId + ">isGaming:" + player.isGaming+">isBanker:" + player.isBanker+">seatId:"+player.seatId);
			}
			
			var seatId:int;
			switch(state) {
				case TableData.GAME_STATE_START:
					//牌局还没开始什么也不传
					break;
				case TableData.GAME_STATE_GRAB_BANK://抢庄阶段
					_grabBankLeaveTime = socket.readByte();
					break;
				case TableData.GAME_STATE_BET://下注阶段
					_betLeaveTime = socket.readByte();
					len = socket.readByte();
					for(i=0; i<len; i++) {
						seatId = socket.readByte();
						player = _tableData.getMatchPlayerBySeatId(seatId);
						if(player) {
							player.isBetMoney = socket.readInt();
						}
					}
					break;
				case TableData.GAME_STATE_SHOW_CARD://比牌阶段
					_selectCardLeaveTime = socket.readByte();
					len = socket.readByte();
					for(i=0; i<len; i++) {
						seatId = socket.readByte();
						player = _tableData.getMatchPlayerBySeatId(seatId);
						player.isBetMoney = socket.readDouble();
					}
					len = socket.readByte();
					var cards:Array;
					var vo:CardVo;
					var cardId:int;
					for(i=0; i<len; i++) {
						seatId = socket.readByte();
						player = _tableData.getMatchPlayerBySeatId(seatId);
						if(player) {
							player.cowNumb = socket.readByte();
							for(j=0; j<5; j++) {
								vo = new CardVo();
								cardId = socket.readShort();
								vo.value = cardId;
								vo.id = cardId&0xff;
								vo.color = cardId>>8;
								if(i<3) {
									vo.selectBoo = true;
								}else {
									vo.selectBoo = false;
								}
								cards.push(vo);
							}
							player.cards = cards;
						}
					}
					break;
				case TableData.GAME_STATE_SETTLEMENT://结算
					break;
			}
			_seatCtrl.showSeat();
		}
		/**
		 *收到用户坐下 
		 * @param socket
		 */
		private function recPlayerSeat(socket:MySocket):void{
			trace("====>收到用户坐下 ");
			socket.readbegin();
			var id:String = String(socket.readInt());
			var seatId:int = socket.readByte();
			var money:int = socket.readInt();
			trace("id:" + id);
			trace("seatId:" + seatId);
			trace("money:" + money);
			trace("<===数据解析完毕");
			var player:Player = _tableData.seatAPlayer(int(id), seatId, money);
			player.seatId = seatId;
			player.tableMoney = money;
			if(player == player) {
				player.isSeat = true;
			}
			_seatCtrl.showASeat(player);
			
		}
		/**
		 * 收到可以抢庄
		 * @param socket
		 */
		private function recCanGrabBanker(socket:MySocket):void{
			trace("====>服务器通知玩家抢庄");
			socket.readbegin();
			_tableData.currentGameState = TableData.GAME_STATE_GRAB_BANK;
			_tableData.clearBankerState(false);		
			
			var t:int = socket.readByte();
			_seatCtrl.showOptionPanle(SeatCtrl.SHOW_OPTION_PANEL_GRAB_BANKER);
			_seatCtrl.showClock(t);
			_seatCtrl.showPublic(t);
		}
		/**
		 *收到玩家抢庄消息 
		 * @param socket
		 */
		private function recPlayerGrabBanker(socket:MySocket):void{
			trace("====>收到玩家抢庄信息");
			socket.readbegin();
			var setId:int = socket.readByte();
			var boo:Boolean = socket.readByte() == 1;
			_seatCtrl.showGrabBankerMotion(setId, boo);
		}
		/**
		 *收到抢庄成功玩家消息 
		 * @param socket
		 */
		private function recBanker(socket:MySocket):void{
			trace("====>抢庄完毕收到庄家");
			socket.readbegin();
			var seatId:int = socket.readByte();
			trace("庄家座位号：" + seatId);
			var player:Player = _tableData.getMatchPlayerBySeatId(seatId);
			player.isBanker = true;
			_seatCtrl.setBanker(seatId);
			trace("<=====解析完毕");
		}
		/**
		 *收到可以下注命令 
		 * @param socket
		 */
		private function recCanBet(socket:MySocket):void{
			trace("====>收到服务器通知玩家可以下注");
			socket.readbegin();
			_tableData.currentGameState = TableData.GAME_STATE_BET;
			//清空玩家比晒状态
			_tableData.clearGamingState(false);
			var len:int = socket.readByte();
			var seatId:int;
			var i:int;
			var p:Player;
			for(i=0; i<len; i++) {
				seatId = socket.readByte();
				p = _tableData.getMatchPlayerBySeatId(seatId);
				p.isGaming = true;
				trace("在比赛中的玩家的id：" + p.gameId + "座位id" + p.seatId);
			}
			_tableData.canBet = true;
			if(!_player.isBanker && _player.isGaming) {
				_seatCtrl.showOptionPanle(SeatCtrl.SHOW_OPTION_PANEL_BET);
				_seatCtrl.showClock(_player.tableVo.betTime);
			}else {
				_seatCtrl.hideAllOptionPanle();
			}
			_seatCtrl.showPublic(_player.tableVo.betTime);
			trace("<==收到服务器通知玩家可以下注完毕");
		}
		/**
		 *收到玩家下注信息 
		 * @param socket
		 */
		private function recPlayerBet(socket:MySocket):void{
			trace("--==>收到玩家下注");
			SoundManager.playerSound(SoundManager.ChipInSound);
			socket.readbegin();
			var id:int = socket.readInt();
			var seatId:int = socket.readByte();
			var bet:int = socket.readInt();
			var player:Player = _tableData.getMatchPlyerById(id);
			player.isBetMoney += bet;
			player.tableMoney -= bet;
			_seatCtrl.refreshUserTableMoney(seatId, player.tableMoney);
			_seatCtrl.showBetMotion(seatId, bet);
		}
		/**
		 * 收到服务器的牌
		 * @param socket
		 */
		private function recGetCard(socket:MySocket):void{
			trace("--==>收到服务器发的牌");
			socket.readbegin();
			_tableData.currentGameState = TableData.GAME_STATE_SHOW_CARD;
			socket.readByte();
			var arr:Array = [];
			var i:int;
			var len:int = 5;
			var card:int;
			var vo:CardVo;
			for(i=0; i<len; i++) {
				vo = new CardVo();
				card = socket.readShort();
				vo.value = card;
				vo.id = card&0xff;
				vo.color = card>>8;
				arr.push(vo);
				trace("牌值：" + vo.value.toString(16) + "<>大小:" + vo.id + "<>花色:" + vo.color);
			}
			_player.cards = arr;
			trace("<===接牌完毕");
			_seatCtrl.showCards(arr, _player.seatId, false);
			dealCard();
		}
		
		/**
		 *发牌 
		 */
		private function dealCard():void {
			var arr:Array = _tableData.getMatchList(false);
			var seatIds:Array = [];
			var p:Player;
			var len:int = arr.length;
			var i:int;
			for(i=0; i<len; i++) {
				p = arr[i];
				seatIds.push(p.seatId-1);
			}
			seatIds.sort();
			_dealCardCtrl.dealCards(seatIds);
		}
		
		/**
		 *发牌动画回调 
		 * @param seatid
		 * @param count
		 */
		private function dealCardCB(seatId:int, count:int):void {
			seatId++;
			if(seatId == _player.seatId) {
				_seatCtrl.showCardByLen(seatId, count);
				if(count == 5 && _player.isGaming) {
					_seatCtrl.showShowCardBtn(true, false);
				}
			}else {
				_seatCtrl.showHideCard(seatId, count);
			}
		}
		
		/**
		 *服务器通知可以选牛按钮 
		 * @param socket
		 * 
		 */
		private function recCanSelectCard(socket:MySocket):void {
			trace("===>服务器通知可以选牛按钮 ");
			_tableData.currentGameState = TableData.GAME_STATE_SHOW_CARD;
			socket.readbegin();
			var t:int = socket.readByte();//选牛倒计时
			_seatCtrl.showClock(t);
			_seatCtrl.showPublic(t);
		}
		/**
		 *收到玩家选牛消息 
		 * @param socket
		 */
		private function recPlayerCowNumb(socket:MySocket):void {
			trace("===>收到玩家选牛消息  ");
			socket.readbegin();
			_tableData.currentGameState = TableData.GAME_STATE_SHOW_CARD;
			var gameId:int = socket.readInt();
			var seatId:int = socket.readByte();
			var cards:Array = [];
			var vo:CardVo;
			var cardId:int;
			var i:int;
			for(i=0; i<5; i++) {
				vo = new CardVo();
				cardId = socket.readShort();
				vo.value = cardId;
				vo.id = cardId&0xff;
				vo.color = cardId>>8;
				if(i<3) {
					vo.selectBoo = true;
				}else {
					vo.selectBoo = false;
				}
				cards.push(vo);
			}
			//显示牌
			var p:Player = _tableData.getMatchPlyerById(gameId);
			//显示牛值
			var cowValue:int = socket.readByte();
			if(cowValue >= DnAlg.COW_NIOU_NIOU) {
				SoundManager.playerSound(SoundManager.Cow);
			}
			p.cowNumb = cowValue;
			_seatCtrl.showCowUi(true, p.seatId, cowValue);
			p.cards = cards;
			_seatCtrl.showCards(p.cards, p.seatId);
			_seatCtrl.showHideCard(p.seatId, 0);
			trace("玩家id:" + gameId);
			trace("牛值:" + p.cowNumb);
			trace("最大牌的值:" + p.getMaxCardVo().id);
			trace("<=============");
		}
		
		/**
		 *收到 游戏结算数据 
		 * @param socket
		 */
		private function recGameResult(socket:MySocket):void {
			_tableData.currentGameState = TableData.GAME_STATE_SETTLEMENT;
			_seatCtrl.showShowCardBtn(false,false);
			trace("===>收到 游戏结算数据  ");
			socket.readbegin();
			var len:int = socket.readByte();
			trace("len==>" + len);
			var i:int;
			var gameId:int;
			var seatId:int;
			var isBanker:Boolean;
			var income:Number;
			for(i=0; i<len; i++) {
				gameId = socket.readInt();
				seatId = socket.readByte();
				isBanker = socket.readByte()==1;
				income = socket.readDouble();
				trace(gameId,isBanker,income);
				if(income > 0 && String(gameId) == _player.gameId) {
					MotionCtrl.getInstance().startMotion(MotionCtrl.winGameMotion);
				}
			}
			_seatCtrl.startSettlement();
			
			_tableData.clearGamingState(false);
		}
		
		/**
		 *重连 
		 * @param socket
		 */
		private function recReconnectInfo(socket:MySocket):void {
			trace("========>收到 重连数据  ");
			socket.readbegin();
			var state:int = socket.readByte();
			_tableData.currentGameState = state;
			var len:int = socket.readByte();
			var id:String;
			var i:int;
			var j:int;
			var player:Player;
			var isGaming:Boolean;
			for(i=0; i<len; i++) {
				id = String(socket.readInt());
				isGaming = socket.readByte() == 1;
				if(id == _player.gameId && isGaming) {
					player = _player;
					
				}else {
					player = new Player();
				}
				player.gameId = id;
				player.isGaming = socket.readByte() == 1;
				player.level = socket.readByte();
				player.titleId = socket.readByte();
				player.seatId = socket.readByte();
				player.gender = socket.readByte();
				player.vipLevel = socket.readByte();
				player.exp = socket.readInt();
				player.giftId = socket.readInt();
				player.tableMoney = socket.readDouble();
				player.gameMoney = socket.readDouble();
				player.userName = socket.readString();
				player.pic = socket.readString();
				if(player.seatId == 10) {
					_tableData.addAWatchPlayer(player);
				}else {
					_tableData.addAMatchPlayer(player);
				}
			}
			
			var seatId:int;
			switch(state) {
				case TableData.GAME_STATE_START:
					//牌局还没开始什么也不传
					break;
				case TableData.GAME_STATE_GRAB_BANK://抢庄阶段
					_grabBankLeaveTime = socket.readByte();
					break;
				case TableData.GAME_STATE_BET://下注阶段
					_betLeaveTime = socket.readByte();
					len = socket.readByte();
					for(i=0; i<len; i++) {
						seatId = socket.readByte();
						player = _tableData.getMatchPlayerBySeatId(seatId);
						if(player) {
							player.isBetMoney = socket.readInt();
						}
					}
					break;
				case TableData.GAME_STATE_SHOW_CARD://比牌阶段
					_selectCardLeaveTime = socket.readByte();
					len = socket.readByte();
					var cards:Array;
					var vo:CardVo;
					var cardId:int;
					for(i=0; i<len; i++) {
						seatId = socket.readByte();
						player = _tableData.getMatchPlayerBySeatId(seatId);
						if(player) {
							player.cowNumb = socket.readByte();
							for(j=0; j<5; j++) {
								vo = new CardVo();
								cardId = socket.readShort();
								vo.value = cardId;
								vo.id = cardId&0xff;
								vo.color = cardId>>8;
								if(i<3) {
									vo.selectBoo = true;
								}else {
									vo.selectBoo = false;
								}
								cards.push(vo);
							}
							player.cards = cards;
						}
					}
					break;
				case TableData.GAME_STATE_SETTLEMENT://结算
					break;
			}
			_seatCtrl.showSeat();
			trace("<=======接收完毕");
		}
		
		/**
		 *播放发牌动画 
		 * @param socket
		 */
		public function recPlayDealCard(socket:MySocket):void {
			trace("========>收到 可以发牌数据  ");
			socket.readbegin();
			if(!_player.isGaming) {
				dealCard();
			}
			trace("<=======接收完毕");
		}
		//============================================================================
		
		override public function start():void {
			var dm:ConfigData = AppDmList.getInstance().configDm;
			_sm.connect(dm.dnServer, dm.dnPort);
		}
		
		/**
		 *连接成功显示桌子 
		 */
		public function showTable():void {
			_seatCtrl.show();
			_tableChat.show();
			AppCtrlList.getInstance().connectCtrl.close();
			AppView.getInstance().tableLayer.visible = true;
		}
		
		/**
		 *关闭桌子 
		 * @return 
		 */
		public function close():void {
			//隐藏桌子层
			_seatCtrl.hide();
			_tableChat.hide();
			AppView.getInstance().tableLayer.visible = false;
			//关闭socket
			_sm.close();
		}
		
		/**
		 *返回大厅 
		 */
		public function goHall():void {
			_player.upSeat();
			_tableData.reset();
			_seatCtrl.hideAllSeat();
			AppCtrlList.getInstance().sceneCtrl.enterHall();
		}
		
		/**
		 *添加侦听器 
		 */
		public function addSocketEvent():void {
			EventCenter.SocketEvent.addEventListener(SocketEvent.CONNECT_SUCCESS, connectionSucess);
			EventCenter.SocketEvent.addEventListener(SocketEvent.CONNECT_FAILURE, connectionFailure);
			EventCenter.SocketEvent.addEventListener(SocketEvent.CONNECT_FAILURE, serverCloseSocketCallBack);
		}
		public function removeSocketEvent():void {
			EventCenter.SocketEvent.removeEventListener(SocketEvent.CONNECT_SUCCESS, connectionSucess);
			EventCenter.SocketEvent.removeEventListener(SocketEvent.CONNECT_FAILURE, connectionFailure);
			EventCenter.SocketEvent.removeEventListener(SocketEvent.CONNECT_FAILURE, serverCloseSocketCallBack);
		}
		
		/**
		 *连接成功 
		 * @param e
		 */
		private function connectionSucess(e:Event):void {
			trace("====>连接成功");
			_sm.login();
		}
		/**
		 *连接失败 
		 * @param e
		 */
		private function connectionFailure(e:Event):void {
			trace("====>连接失败");
		}
		/**
		 *服务器主动关闭连接 
		 * @param e
		 */
		private function serverCloseSocketCallBack(e:Event):void {
			trace("====>服务器主动关闭连接");
			AppCtrlList.getInstance().alertCtrl.sysMsgBox(Language.ERROR_CODE[0]);
			goHall();
		}
	}
}